package com.geeyuu.android.utils;

public class ProtocolUtil {

    public static int byteArrayToUShort(byte[] b, int offset) {
        int value = 0;
        for (int i = 0; i < 2; i++) {
            int shift = (2 - 1 - i) * 8;
            value += (b[i + offset] & 0x000000FF) << shift;
        }
        return value;
    }

    public static long byteArrayToUInt(byte[] b, int offset) {
        long value = 0;
        for (int i = 0; i < 4; i++) {
            int shift = (4 - 1 - i) * 8;
            value += (b[i + offset] & 0x000000FF) << shift;
        }
        return value;
    }
    
    public static byte[] uShortToByteArray(int i) {
        byte[] b = new byte[2]; 
        b[0] = (byte) ((i >> 8) & 0xFF);
        b[1] = (byte) (i & 0xFF);
        return b;
    }

    public static byte[] uIntToByteArray(long l) {
        byte[] b = new byte[4]; 
        b[0] = (byte) ((l >> 24) & 0xFF);
        b[1] = (byte) ((l >> 16) & 0xFF);
        b[2] = (byte) ((l >> 8) & 0xFF);
        b[3] = (byte) (l & 0xFF);
        return b;
    }
    
}
